package com.mangguo.tingjian.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class DateUtils {

    public static Integer getTodayInt(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return Integer.valueOf(simpleDateFormat.format(new Date()));
    }

    public static Integer getTodayMonth(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        return Integer.valueOf(simpleDateFormat.format(new Date()));
    }

    public static Integer getTodayYear(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        return Integer.valueOf(simpleDateFormat.format(new Date()));
    }

    public static Integer getYearInt(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String dateStr = simpleDateFormat.format(date);
        return Integer.valueOf(dateStr);
    }

    public static Integer getMonthInt(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
        String dateStr = simpleDateFormat.format(date);
        return Integer.valueOf(dateStr);
    }

    public static Integer getWeekInt(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        return cl.get(Calendar.WEEK_OF_MONTH);
    }

    public static List<Integer> getBetweenDates(Integer startInt, Integer endInt){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date start = null;
        Date end = null;
        try {
            start = simpleDateFormat.parse(startInt.toString());
            end = simpleDateFormat.parse(endInt.toString());
        } catch (ParseException e) {
            log.error("解析日期错误", e);
        }
        List<Integer> result = new LinkedList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
//        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        tempEnd.add(Calendar.DAY_OF_YEAR, 1);
        while (tempStart.before(tempEnd)) {
            result.add(Integer.valueOf(simpleDateFormat.format(tempStart.getTime())));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    public static Integer addDay(Integer dateInt, Integer addDay){
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(dateInt.toString());
        } catch (ParseException e) {
            log.error("日期转化错误", e);
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, addDay);
        return Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()));
    }
}
